﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace IReklamo
{
    public class ComplaintsController : Controller
    {
        public JsonResult CreateComplaint(string complainee, string comment, Guid? createComplaintId, string title)
        {
            var complaint = new Complaint
            {
                Id = null,
                Title = title,
                Comment = comment,
                Complainee = complainee
            };

            var repository = new ComplaintRepository();
            repository.AddComplaint(complaint);

            return Json(new { complaintId = complaint.Id }, JsonRequestBehavior.AllowGet);
        }

        public void BroadcastComplaint(int complaintId)
        {
            var request = new PusherRESTDotNet.ObjectPusherRequest(
                "feed_channel",
                "complaint_added", new { complaintId = complaintId, }
                );
            WebContext.PusherProvider.Trigger(request);
        }

        public JsonResult Support(int complaintId)
        {
            var repository = new ComplaintRepository();
            repository.AddSupport(complaintId);

            var request = new PusherRESTDotNet.ObjectPusherRequest(
                string.Concat("complaint_channel_", complaintId),
                "changed",
                new { complaintId = complaintId, type ="support" }
            );
            WebContext.PusherProvider.Trigger(request);

            return Json(new {}, JsonRequestBehavior.AllowGet);
        }

        public JsonResult Dislike(int complaintId)
        {
            var repository = new ComplaintRepository();
            repository.AddDislike(complaintId);

            var request = new PusherRESTDotNet.ObjectPusherRequest(
                string.Concat("complaint_channel_", complaintId),
                "changed",
                new { complaintId = complaintId, type = "dislike" }
            );
            WebContext.PusherProvider.Trigger(request);

            return Json(new {}, JsonRequestBehavior.AllowGet);
        }

        public JsonResult AddComment(int complaintId, string message)
        {
            int total = 0;
            var repository = new ComplaintRepository();
            var comment = new Comment
            {
                ComplaintId = complaintId,
                Message = message,
                Date = DateTime.UtcNow
            };
            repository.AddComment(comment);

            var request = new PusherRESTDotNet.ObjectPusherRequest(
                string.Concat("complaint_channel_", complaintId),
                "changed",
                new
                {
                    type ="comment",
                    commentId = comment.Id,
                    complaintId = complaintId,
                    message = message,
                    name = string.Empty
                });
            WebContext.PusherProvider.Trigger(request);

            return Json(new { }, JsonRequestBehavior.AllowGet);
        }
        
        public JsonResult GetComments(int complaintId)
        {
            var repository = new ComplaintRepository();
            var comments = repository.GetComments(complaintId);
            return Json(
                    comments.Select(c => new
                    {
                        message = c.Message,
                        id = c.Id
                    }).ToArray()
                , JsonRequestBehavior.AllowGet);
        }

        public ViewResult GetComplaint(int complaintId)
        {
            var repository = new ComplaintRepository();
            var complaint = repository.GetComplaint(complaintId);
            return View("Complaint", complaint);
        }

    }
}
